<%= javascript_include_tag 'routes' %>
<% select_option_count = 7 %>

<div id="content">
	<div id="top">
		<h3><%= @route.name %></h3>
		<%= render :partial => '/partials/help_link', :locals => {:path => {:controller => "help", :action => "routes", :anchor => "edit"}} %>
	</div>

	<%= error_messages_for :route_recurrence_rule %>
	<div id="couriers">
		<h2>Daily Routes</h2>
		<% @route.couriers.sort {|x,y| x.real_name <=> y.real_name}.each do |courier| %>
			<% select_offset_id = "select_offset_for_#{courier.id}" %>
			<% select_offset = params[select_offset_id].to_i || 0 %>
			<% many_future_route_instances = @route.future_route_instances_for(courier.id, select_option_count + select_offset + 1) %>
			<% future_route_instances = many_future_route_instances[select_offset...(select_offset + select_option_count)] || [] %>			   
			<% if future_route_instances.size > 0 %>
				<div class="route_courier">
					<% select_id = nil %>
					<div class="top">
						<h3><%= courier.real_name %></h3>
						<span class="courier_header">
							<%= hidden_field_tag select_offset_id, select_offset %>
							<select id="<%= "select_for_#{courier.id}" %>" onchange="update_selected_route_instance(<%= courier.id %>)">
								<% future_route_instances.each do |instance| %>
									<option value="<%= instance.day %>">
										<%= instance.new_record? ? "" : "*" %><%= instance.day.strftime("%a %b %e") %>
									</option>
								<% end %>
								<% if many_future_route_instances.size > future_route_instances.size %>
									<option>See More</option>
								<% end %>
								<% unless select_offset == 0 %>
									<option>Reset List</option>
								<% end %>
								</select>
						</span>
					</div>
					<% future_route_instances.each do |route_instance| %>
						<div id="<%= "#{courier.id}_#{route_instance.day}" %>" class="route_instance_for_<%= courier.id %> route_instance">
							<% form_for route_instance do |f| %>
								<% if route_instance.instantiated_by %>
									<p>(changed by <%= route_instance.instantiated_by.real_name %> on <%= route_instance.updated_at.to_date.strftime("%a %b %e") %>)</p>
								<% end %>
								<%= f.hidden_field :day %>
								<% route_instance.instantiated_by = @current_user %>
								<%= f.hidden_field :instantiated_by_id %>
								<%= f.hidden_field :route_id %>
								<%= f.hidden_field :courier_id %>
								<ol id=<%= "#{courier.id}_#{route_instance.day}_stop_list" %>>
									<%= render :partial => 'stop_instance', :collection => route_instance.stop_instances.sort {|x,y| x.stop_number <=> y.stop_number } %>
								</ol>
								
								<%= add_stop_link "Add a Stop", "#{courier.id}_#{route_instance.day}_stop_list", 'stop_instance', route_instance.stop_instances.new %>
								
								<% rules = @route.route_recurrence_rules.select {|x| x.courier == courier} %>
								<% if not route_instance.new_record? %>
									<%= link_to "reset to default (this day only)", route_instance, :method => 'delete', :id => route_instance.id %>
								<% elsif rules.size > 0 %>
									<%= link_to "unassign (recurring)", destroy_route_recurrence_rule_path(:rule_id => rules.first.id), :method => :delete %>
								<% end %>
								<%= f.submit "Update" %>
							<% end %>
						</div>
					<% end %>
					<%= javascript_tag "update_selected_route_instance(#{courier.id})" %>				
				</div>
			<% end %>
		<% end %>

		<% if @unassigned_couriers.size > 0 %>
			<h2>Assign Another Courier</h2>
			<% form_for @route_recurrence_rule do |f| %>
				<%= f.hidden_field :route_id %>
				<%= f.collection_select :courier_id, @unassigned_couriers, :id, :real_name %>
				
				<div class="days">
					<% RouteRecurrenceRule::Days.each do |day| %>
						<%= f.check_box day %>
						<%= f.label day %>
					<% end %>
				</div>
				<%= submit_tag 'Add' %>
			<% end %>
		<% end %>
	</div>
	
	<div id="stops">
		<h2>Original Route</h2>
		<ol>
		<% @route.stops.sort {|x,y| x.stop_number <=> y.stop_number}.each do |stop| %>
			<li><%= stop.stop_number %>. <%= stop.location.name %></li>
		<% end %>
		</ol>
		<% if @recurs_on.size > 0 %>
			<p>
				Route assigned to recur on: <%= @recurs_on.map {|x| x.to_s.capitalize }.join ', ' %>
			</p>
		<% end %>
		<% if @route.notes.size > 0 %>
			<h2>Notes</h2>
			<p><%= @route.notes %></p>
		<% end %>
	</div>
</div>